package 周赛.L342;

import java.util.Arrays;

/**
 * @author chenhong
 * @version 1.0
 * @description TODO
 * @date 2023/4/23 10:37
 */
public class L6390 {
    public static void main(String[] args) {
        new L6390().getSubarrayBeauty(new int[]{-1,-2,-3,-4,-5}, 2, 2);
    }

    /*public int[] getSubarrayBeauty(int[] nums, int k, int x) {
        int n = nums.length;
        int[] ans = new int[n - k + 1];
        int left = 0;
        if (x == 1) {
            PriorityQueue<Integer> queue_min = new PriorityQueue<>();
            for (int i = 0; i < k; i++) {
                queue_min.offer(nums[i]);
            }
            ans[0] = queue_min.peek() < 0 ? queue_min.peek() : 0;
            for (int i = k; i < n; i++) {
                queue_min.remove(nums[left]);
                queue_min.offer(nums[i]);
                left ++;
                ans[ i - k + 1] = queue_min.peek() < 0 ? queue_min.peek() : 0;
            }
            return ans;
        }
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for (int i = 0; i < k; i++) {
            if (queue.size() < x)
                queue.offer(nums[i]);
            else if (nums[i] < queue.peek()) {
                queue.poll();
                queue.offer(nums[i]);
            }
        }
        ans[0] = queue.peek() < 0 ? queue.peek() : 0;
        for (int i = k ; i < n; i++) {
            if (queue.contains(nums[left])) {
                queue.remove(nums[left]);
                queue.offer(nums[i]);
            } else if (nums[i] < queue.peek()) {
                queue.poll();
                queue.offer(nums[i]);
            }
            ans[i - k + 1] = queue.peek() < 0 ? queue.peek() : 0;
            left++;
        }
        return ans;
    }*/
    public int[] getSubarrayBeauty(int[] nums, int k, int x) {
        int n = nums.length;
        int[] ans = new int[n - k + 1];
        Arrays.sort(nums, 0, k);
        ans[0] = Math.min(nums[x - 1], 0);
        int left = 1;
        for (int i = k; i < n; i++) {
            Arrays.sort(nums, left, i + 1);
            ans[i - k + 1] = Math.min(nums[x - 1], 0);
            left++;
        }
        return ans;
    }
}
